import { useRequest } from 'umi';
import {
  queryAreaListAPI,
  addDoorAPI,
  queryPlanModeAPI,
  queryDoorInfoAPI,
  editDoorAPI,
  deleteDoorAPI,
  queryControllerTypeAPI,
  queryDoorOpenTypeAPI,
  sentDownPlanModeAPI,
  queryDoorListAPI,
  queryDoorGroupListAPI,
  addDoorGroupAPI,
  deleteDoorGroupAPI,
  editDoorGroupAPI,
  queryDoorListFormDoorGroupIdAPI,
  addDoorToDoorGroupAPI,
} from './services';
import { createModel } from 'hox';
import { useState } from 'react';
import { DataNode } from 'antd/lib/tree';
import { AreaListReturnProps } from './API';
type AreaListProps = DataNode & { code?: string };
interface HoxModelProps {
  areaList: AreaListProps[];
  selectedRowKeys: (string | number)[];
  formAreaList: AreaListReturnProps[];
  isShowControllerTable: boolean;
  selectedDoorGroup: number | null;
  doorGroupInfo?: { id?: number };
  info: { id?: number; [key: string]: any };
}
function useHoxModel() {
  const [state, setState] = useState<HoxModelProps>({
    areaList: [],
    selectedRowKeys: [],
    formAreaList: [],
    isShowControllerTable: false,
    selectedDoorGroup: null,
    doorGroupInfo: {},
    info: {},
  });
  const dispatch = (payload: Partial<HoxModelProps>) => {
    setState((pre) => ({ ...pre, ...payload }));
  };

  //获取表单区域接口
  const { run: getFormAreaList } = useRequest(queryAreaListAPI, {
    manual: true,
    onSuccess: (res) => {
      if (res.result === 0) {
        let data = res?.data;
        const formAreaList = data.map((item: any) => {
          return {
            label: item.name,
            value: item.id,
            isLeaf: item.isRegion,
            code: item.code,
          };
        });
        dispatch({ formAreaList });
      }
    },
    formatResult: (res) => res,
  });
  //下发计划模板
  const { run: sentDownPlanMode } = useRequest(sentDownPlanModeAPI, {
    manual: true,
  });
  //获取控制器类型接口
  const { run: getControllerType, data: controllerType } = useRequest(queryControllerTypeAPI, {
    manual: true,
  });
  //获取控制器类型接口
  const { run: getDoorOpenType, data: doorOpenType } = useRequest(queryDoorOpenTypeAPI, {
    manual: true,
  });
  //获取计划模板列表接口
  const { run: getPlanMode, data: planMode } = useRequest(queryPlanModeAPI, {
    manual: true,
  });
  //获取左边区域接口
  const { run: getAreaList } = useRequest(queryAreaListAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //获取门组接口
  const {
    run: getDoorGroupList,
    data: doorGroupList,
    loading: getDoorGroupListLoading,
  } = useRequest(queryDoorGroupListAPI, {
    manual: true,
    onSuccess: (data) => {
      if (!state.selectedDoorGroup && data?.list && data?.list[0]) {
        dispatch({ selectedDoorGroup: data?.list[0].id });
      }
    },
  });
  //获取门设备列表接口
  const { run: getFormDoorList, data: formDoorList } = useRequest(queryDoorListAPI, {
    manual: true,
  });
  //编辑门组接口
  const { run: editDoorGroup } = useRequest(editDoorGroupAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //删除门组接口
  const { run: deleteDoorGroup } = useRequest(deleteDoorGroupAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //添加门到门组里
  const { run: addDoorToDoorGroup } = useRequest(addDoorToDoorGroupAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //添加门组接口
  const { run: addDoorGroup } = useRequest(addDoorGroupAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //添加门接口
  const { run: addDoor } = useRequest(addDoorAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //删除用户接口
  const { run: deleteDoor } = useRequest(deleteDoorAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //修改用户接口
  const { run: editDoor } = useRequest(editDoorAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //获取用户详情接口
  const { run: getDoorInfo, data: doorInfo, loading: doorInfoLoading } = useRequest(
    queryDoorInfoAPI,
    {
      manual: true,
    },
  );
  //获取门设备列表接口
  const { run: getDoorList, data: doorList } = useRequest(queryDoorListFormDoorGroupIdAPI, {
    manual: true,
  });

  return {
    ...state,
    dispatch,
    addDoor,
    getAreaList,
    editDoor,
    getDoorList,
    doorList,
    getDoorInfo,
    doorInfo,
    deleteDoor,
    doorInfoLoading,
    getPlanMode,
    getControllerType,
    controllerType,
    doorOpenType,
    getDoorOpenType,
    sentDownPlanMode,
    planMode,
    addDoorToDoorGroup,
    getFormAreaList,
    getDoorGroupList,
    doorGroupList,
    addDoorGroup,
    editDoorGroup,
    deleteDoorGroup,
    getFormDoorList,
    formDoorList,
    getDoorGroupListLoading,
  };
}
export default createModel(useHoxModel);
